package com.hengda.museumonline.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.BaseAdapter;
import com.hengda.museumonline.adapter.BaseViewHolder;
import com.hengda.museumonline.adapter.ErrorAdapter;
import com.hengda.museumonline.entity.EngineeringBean;
import com.hengda.museumonline.entity.TechnologyBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.http.GetData;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.ui.PcrContentActivity;
import com.hengda.museumonline.view.EngineeringRecyclerView;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.List;

/**
 * @author：JFWU on 2016/9/27 10:54
 * @E-mail：jeffy12138@126.com
 */
public class TechnologyFragment extends Fragment {
    private View fl_content;
    private EngineeringRecyclerView recyclerView;
    private ImageView loading_bg;
    private MainActivity mainActivity;
    private TechnologyBean technologyBean1 = new TechnologyBean();
    private TechnologyBean technologyBean2 = new TechnologyBean();
    private MyAdapter mAdapter;
    private int i = 1;

    public TechnologyFragment(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fl_content = View.inflate(mainActivity, R.layout.fragment_technology, null);
        loading_bg = (ImageView) fl_content.findViewById(R.id.loading_bg);
        recyclerView = (EngineeringRecyclerView) fl_content.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        getDataFromServer();
        return fl_content;
    }

    private void getDataFromServer() {
        Request request = GetData.getNews(mainActivity, MyApplication.url + "relic/project", "a",
                PackageUtil.getAppPackageInfo(mainActivity).versionName, 2, 0, 6);
        CallServer.getRequestInstance().add(mainActivity, 0, request, listener);
    }

    OnResponseListener listener = new OnResponseListener() {
        @Override
        public void onStart(int what) {

        }

        @Override
        public void onSucceed(int what, Response response) {
            int responseCode = response.getHeaders().getResponseCode();// 服务器响应码
            System.out.println("结果：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + response.get().toString());
            Gson gson = new Gson();
            if (0 == what) {
                System.out.println("结果：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + response.get().toString());
                technologyBean1 = gson.fromJson(response.get().toString(), TechnologyBean.class);
                if (technologyBean1 != null && technologyBean1.getData() != null && technologyBean1.getData().size() > 0) {
                    loading_bg.setVisibility(View.INVISIBLE);
                    mAdapter = new MyAdapter(mainActivity, recyclerView, technologyBean1.getData(), R.layout.technology_item);
                    View view = View.inflate(mainActivity, R.layout.listview_header, null);
                    mAdapter.addHeadView(view);
                    recyclerView.setAdapter(mAdapter);
                    mAdapter.setOnRefreshListener(new BaseAdapter.OnRefreshListener() {
                        @Override
                        public void onPullDownRefresh() {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    mAdapter.onRefreshFinish();
                                }
                            }, 3000);
                        }
                    });
                    mAdapter.setOnLoadMoreListener(new BaseAdapter.OnLoadMoreListener() {
                        @Override
                        public void onLoadMore() {
                            Request request = GetData.getNews(mainActivity, MyApplication.url + "relic/project", "a",
                                    PackageUtil.getAppPackageInfo(mainActivity).versionName, 1, 6 * i, 6);
                            i += 1;
                            CallServer.getRequestInstance().add(mainActivity, 1, request, listener);
                        }
                    });
                } else {
                    recyclerView.setAdapter(new ErrorAdapter(mainActivity));
                }
            } else if (1 == what) {
                technologyBean2 = gson.fromJson(response.get().toString(), TechnologyBean.class);
                if (technologyBean2.getData() == null) {
                    mAdapter.setLoading(false);
                } else {
                    mAdapter.addAll(technologyBean2.getData());
                }
            }
        }

        @Override
        public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
            System.out.println("失败：" + "responseCode:" + responseCode + ",what:" + what + ",.....:" + exception);
            recyclerView.setAdapter(new ErrorAdapter(mainActivity));
        }

        @Override
        public void onFinish(int what) {

        }
    };

    class MyAdapter extends BaseAdapter<TechnologyBean.DataBean> {

        public MyAdapter(Context mContext, EngineeringRecyclerView recyclerView, List<TechnologyBean.DataBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(final Context mContext, RecyclerView.ViewHolder holder, final TechnologyBean.DataBean dataBean) {
            ((BaseViewHolder) holder).setText(R.id.title, dataBean.getTitle());
            ((BaseViewHolder) holder).setText(R.id.date, dataBean.getDate().substring(0, 10));
            if ("3".equals(dataBean.getTypeid())) {
                //((BaseViewHolder) holder).setBackgroundColor(R.id.type, R.color.tuiguang);
                ((BaseViewHolder) holder).setBackgroundResource(R.id.type, R.color.tuiguang);
            } else if ("4".equals(dataBean.getTypeid())) {
               // ((BaseViewHolder) holder).setBackgroundColor(R.id.type, R.color.shebei);
                ((BaseViewHolder) holder).setBackgroundResource(R.id.type, R.color.shebei);
            } else if ("5".equals(dataBean.getTypeid())) {
             //   ((BaseViewHolder) holder).setBackgroundColor(R.id.type, R.color.chanpin);
                ((BaseViewHolder) holder).setBackgroundResource(R.id.type, R.color.chanpin);
            }
            ((BaseViewHolder) holder).setText(R.id.type, dataBean.getType());
            ((BaseViewHolder) holder).setText(R.id.post_hits, dataBean.getPost_hits());
            ((BaseViewHolder) holder).setImageUrl(R.id.pic_url, dataBean.getPic_url(), R.mipmap.cheese_5);
            ((BaseViewHolder) holder).setOnClickListener(R.id.technology_item, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", dataBean.getId());
                    mainActivity.startActivity(mContext, PcrContentActivity.class, bundle);
                }
            });
        }


    }

}
